package com.hs.communication.common.fallback;

import com.hs.common.core.domain.Result;
import com.hs.communication.common.entity.Inverter;
import com.hs.communication.common.entity.InverterData;
import com.hs.communication.common.entity.vo.DeviceVo;
import com.hs.communication.common.feign.RemoteInverterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class RemoteInverterFallbackFactory implements FallbackFactory<RemoteInverterService> {

    @Override
    public RemoteInverterService create(Throwable cause) {
        log.error("获取充电桩统计数据远程服务调用失败:{}", cause.getMessage());
        return new RemoteInverterService() {
            @Override
            public Result<DeviceVo> getStatistics(String source) {
                return Result.fail(DeviceVo.builder().build());
            }

            @Override
            public Result<Boolean> insert(InverterData inverterData, String source) {
                System.out.println("插入实时数据失败,触发降级");
                return Result.fail(false);
            }

            @Override
            public Result<Boolean> updateStatus(Inverter inverter, String source) {
                System.out.println("更改在线/离线状态失败sn=" + inverter.getSn());
                return Result.fail(false);
            }
        };
    }
}
